package com.sq.partner.manager.dao.impl;

/**
 * 交易记录
 * @author Kang Zhiqiang
 *
 */

import java.math.BigDecimal;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import com.sq.partner.manager.dao.IGatewayPolicyDao;
import com.sq.partner.manager.mybatis.model.GatewayPolicy;
import com.sq.partner.manager.util.DecimalUtil;
import com.sq.partner.manager.util.LogUtil;
import com.sq.partner.manager.util.Page;
import com.sq.partner.manager.util.StrUtil;

@Repository
public class GatewayPolicyDaoImpl extends BaseDaoImpl implements IGatewayPolicyDao {
	
	private static Logger logger = LogUtil.getLog(BaseDaoImpl.class);

	/*public Page queryLifePolicyPageByCriteria(Page page) throws Exception {
		
		Map<String,Object> map = (Map<String, Object>) page.getType();
		List<GatewayPolicy> countLs = queryList("queryPolicyCountOfLifeByMap", map);
		long total = 0;
		//有效保单笔数(承保状态)
		int countOfValid = 0;
		//有效保单总保费(承保状态)
		BigDecimal totalPremiumOfValid = new BigDecimal(0);
		//注销保单笔数(已注销状态)
		int countOfCancel = 0;
		//注销保单总保费(已注销状态)
		BigDecimal totalPremiumOfCancel = new BigDecimal(0);
		for(GatewayPolicy tem: countLs){
			total+=tem.getCountTotal();
			if("2".equals(tem.getStateId())){
				countOfValid = tem.getCountTotal();
				totalPremiumOfValid = tem.getPremiumValid();
			}
			if("3".equals(tem.getStateId())){
				countOfCancel = tem.getCountTotal();
				totalPremiumOfCancel = tem.getPremiumValid();
			}
		}
		List<GatewayPolicy> list = queryList("queryPolicyPageOfLifeByMap", map);
		//非即时起保保单保险止期应该是数据库所存日期的24:00:00
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
		for (GatewayPolicy tem : list) {
			GatewayPolicy policy = tem;
			String endDateStr = policy.getEndDateStr();
			if(StrUtil.isNotEmpty(endDateStr)){
				policy.setEndDateStr(sdf.format(sdf.parse(endDateStr)));
			}
		}
		
		page.setTotal(total);
		page.setData(list);
		
		Map<String,Object> resultMap = new HashMap<String, Object>();
		resultMap.put("countOfValid", countOfValid);
		resultMap.put("totalPremiumOfValid", DecimalUtil.toDouble(totalPremiumOfValid, 2));
		resultMap.put("countOfCancel", countOfCancel);
		resultMap.put("totalPremiumOfCancel", DecimalUtil.toDouble(totalPremiumOfCancel, 2));
		page.setMap(resultMap);
		
		return page;
	}*/

	@Override
	public List<Map<String, Object>> queryLifePolicyListByCriteria(
			Map<String, Object> map) throws Exception {
		
		List<Map<String, Object>> lifePolicyList = queryList("queryPolicyListOfLifeByMap", map);
		
		//非即时起保保单保险止期应该是数据库所存日期的24:00:00
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
		for (Map<String, Object> m : lifePolicyList) {
			String endDateStr = (String)m.get("END_DATE");
			
			if(StrUtil.isNotEmpty(endDateStr) && !"null".equals(endDateStr)){
				m.put("END_DATE", sdf.format(sdf.parse(endDateStr)));
			}
		}

		return lifePolicyList;
	}
	
	@Override
	public List<Map<String, Object>> queryLifePolicyEinvoiceListByCriteria(
			Map<String, Object> map) throws Exception {
		map.put("einvoiceStatus", "2");
		List<Map<String, Object>> lifePolicyList = queryList("queryPolicyEinvoiceListOfLifeByMap", map);
		
		//非即时起保保单保险止期应该是数据库所存日期的24:00:00
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
		for (Map<String, Object> m : lifePolicyList) {
			String endDateStr = (String)m.get("END_DATE");
			
			if(StrUtil.isNotEmpty(endDateStr) && !"null".equals(endDateStr)){
				m.put("END_DATE", sdf.format(sdf.parse(endDateStr)));
			}
		}

		return lifePolicyList;
	}

	@Override
	public GatewayPolicy queryPolicyInfoByPolNo(String polNo) throws Exception {
		return (GatewayPolicy)this.query("queryPolicyDetailInfoByPolNo", polNo);
	}
	@Override
	public GatewayPolicy queryScanPolicyInfoByPolNo(String polNo) throws Exception {
		return (GatewayPolicy)this.query("queryScanPolicyInfoByPolNo", polNo);
	}
	  @Override
	    public Map<String, Object> queryNocarPolicyAll(Map<String, String> map) throws Exception {
		  Map<String,Object> result = new HashMap<String, Object>();
		  List<GatewayPolicy> countLs = queryList("queryPolicyCountOfNocarByMap", map);
			//有效保单笔数(承保状态)
			int countValidPolicy = 0;
			int countOfCancel = 0;
			//有效保单总保费(承保状态)
			BigDecimal totalPremium = new BigDecimal(0);
			BigDecimal totalPremiumOfCancel = new BigDecimal(0);
			for(GatewayPolicy tem: countLs){
				if("2".equals(tem.getStateId())){
					countValidPolicy = tem.getCountTotal();
					totalPremium = tem.getPremiumValid();
				}
				if("3".equals(tem.getStateId())){
					countOfCancel = tem.getCountTotal();
					totalPremiumOfCancel = tem.getPremiumValid();
				}
			}
			result.put("countValidPolicy", countValidPolicy);
			result.put("totalPremium", totalPremium);
			result.put("countOfCancel", countOfCancel);
			result.put("totalPremiumOfCancel", totalPremiumOfCancel);
			return result;
	    }

	@Override
	public Map<String, Object> queryFuelGasPolicyAll(Map<String, String> map) throws Exception {
		Map<String,Object> result = new HashMap<String, Object>();
		List<GatewayPolicy> countLs = queryList("queryPolicyCountOfFuelGasByMap", map);
		//有效保单笔数(承保状态)
		int countValidPolicy = 0;
		int countOfCancel = 0;
		//有效保单总保费(承保状态)
		BigDecimal totalPremium = new BigDecimal(0);
		BigDecimal totalPremiumOfCancel = new BigDecimal(0);
		for(GatewayPolicy tem: countLs){
			if("2".equals(tem.getStateId())){
				countValidPolicy = tem.getCountTotal();
				totalPremium = tem.getPremiumValid();
			}
			if("3".equals(tem.getStateId())){
				countOfCancel = tem.getCountTotal();
				totalPremiumOfCancel = tem.getPremiumValid();
			}
		}
		result.put("countValidPolicy", countValidPolicy);
		result.put("totalPremium", totalPremium);
		result.put("countOfCancel", countOfCancel);
		result.put("totalPremiumOfCancel", totalPremiumOfCancel);
		return result;
	}

	@Override
	public Page queryNocarPolicyPageByCriteria(Page page) throws Exception {
		Map<String,Object> map = (Map<String, Object>) page.getType();
		long query = (Long) this.query("queryPolicyCountOfNocarByMap1", map);
		List<GatewayPolicy> list = queryList("queryPolicyPageOfNocarByMap", map);
		//非即时起保保单保险止期应该是数据库所存日期的24:00:00
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
		for (GatewayPolicy tem : list) {
			GatewayPolicy policy = tem;
			String endDateStr = policy.getEndDateStr();
			if(StrUtil.isNotEmpty(endDateStr)){
				policy.setEndDateStr(sdf.format(sdf.parse(endDateStr)));
			}
		}
		page.setTotal(query);
		page.setData(list);
		return page;
	}


	@Override
	public Page queryFuelGasPolicyPageByCriteria(Page page) throws Exception {
		Map<String,Object> map = (Map<String, Object>) page.getType();
		long query = (Long) this.query("queryPolicyCountOfFuelGasByMap1", map);
		List<GatewayPolicy> list = queryList("queryPolicyPageOfFuelGasByMap", map);
		//非即时起保保单保险止期应该是数据库所存日期的24:00:00
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
		for (GatewayPolicy tem : list) {
			GatewayPolicy policy = tem;
			String endDateStr = policy.getEndDateStr();
			if(StrUtil.isNotEmpty(endDateStr)){
				policy.setEndDateStr(sdf.format(sdf.parse(endDateStr)));
			}
		}
		page.setTotal(query);
		page.setData(list);
		return page;
	}



	@Override
	public List<Map<String, Object>> queryNocarPolicyListByCriteria(
			Map<String, Object> map) throws Exception {
		
		List<Map<String, Object>> nocarPolicyList = queryList("queryPolicyListOfNocarByMap", map);
		
		//非即时起保保单保险止期应该是数据库所存日期的24:00:00
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
		for (Map<String, Object> m : nocarPolicyList) {
			String endDateStr = (String)m.get("END_DATE");
			
			if(StrUtil.isNotEmpty(endDateStr) && !"null".equals(endDateStr)){
				m.put("END_DATE", sdf.format(sdf.parse(endDateStr)));
			}
		}

		return nocarPolicyList;
	}

	@Override
	public List<Map<String, Object>> queryFuelGasPolicyListByCriteria(
			Map<String, Object> map) throws Exception {

		List<Map<String, Object>> nocarPolicyList = queryList("queryFuelGasPolicyListByCriteria", map);

		//非即时起保保单保险止期应该是数据库所存日期的24:00:00
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
		for (Map<String, Object> m : nocarPolicyList) {
			String endDateStr = (String)m.get("END_DATE");

			if(StrUtil.isNotEmpty(endDateStr) && !"null".equals(endDateStr)){
				m.put("END_DATE", sdf.format(sdf.parse(endDateStr)));
			}
		}

		return nocarPolicyList;
	}

	@Override
	public List<Map<String, Object>> queryCarPolicyListByCriteria(
			Map<String, Object> map) throws Exception {

		
/*		List<Map<String, Object>> carPolicyList = queryList("queryPolicyListOfCarByMap", map);*/
		
	/*	//非即时起保保单保险止期应该是数据库所存日期的24:00:00
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
		for (Map<String, Object> m : carPolicyList) {
			String endDateStr = (String)m.get("END_DATE");
			
			if(StrUtil.isNotEmpty(endDateStr) && !"null".equals(endDateStr)){
				m.put("END_DATE", sdf.format(sdf.parse(endDateStr)));
			}
		}*/

		return queryList("queryPolicyListOfCarByMap", map);
	
	}

	@Override
	public Page queryCarPolicyPageByCriteria(Page page) throws Exception {
		
		Map<String,Object> map = (Map<String, Object>) page.getType();
		List<GatewayPolicy> countLs = queryList("queryPolicyCountOfCarByMap", map);
		long total = 0;
		//有效保单笔数(承保状态)
		int countOfValid = 0;
		//有效保单总保费(承保状态)
		BigDecimal totalPremiumOfValid = new BigDecimal(0);
		for(GatewayPolicy tem: countLs){
			total+=tem.getCountTotal();
			if("2".equals(tem.getStateId())){
				countOfValid = tem.getCountTotal();
				totalPremiumOfValid = tem.getPremiumValid();
			}
		}
		List<GatewayPolicy> list = queryList("queryPolicyPageOfCarByMap", map);
		//非即时起保保单保险止期应该是数据库所存日期的24:00:00
	/*	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
		for (GatewayPolicy tem : list) {
			GatewayPolicy policy = tem;
			String endDateStr = policy.getEndDateStr();
			if(StrUtil.isNotEmpty(endDateStr)){
				policy.setEndDateStr(sdf.format(sdf.parse(endDateStr)));
			}
		}*/
		
		page.setTotal(total);
		page.setData(list);
		
		Map<String,Object> resultMap = new HashMap<String, Object>();
		resultMap.put("countOfValid", countOfValid);
		resultMap.put("totalPremiumOfValid", DecimalUtil.toDouble(totalPremiumOfValid, 2));
		page.setMap(resultMap);
		
		return page;
	
	}

	@Override
	public GatewayPolicy queryNocarPolicyInfoByPolNo(String polNo)
			throws Exception {
		return (GatewayPolicy) this.query("queryNocarPolicyInfoByPolNo", polNo);
	}
		
	//查询已开发票的保单数量和信息--人险
	@SuppressWarnings("unchecked")
	@Override
	public Page queryLifePolicyEinvoiceInfoByPage(Page page) throws SQLException {
		@SuppressWarnings("unchecked")
		Map<String,Object> map = (Map<String, Object>) page.getType();
		//设置条件：已开票
		map.put("einvoiceStatus", "2");
		//已开发票总数
		Integer count = (Integer) query("queryPolicyEinvoiceCountOfLifeByMap", map);
//查询出总数为0
		if(count == null ){
			count = 0;
		}
		@SuppressWarnings("unchecked")
		List<GatewayPolicy> list = queryList("queryPolicyEinvoicePageOfLifeByMap", map);
		//非即时起保保单保险止期应该是数据库所存日期的24:00:00
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
		for (GatewayPolicy tem : list) {
			GatewayPolicy policy = tem;
			String endDateStr = policy.getEndDateStr();
			if(StrUtil.isNotEmpty(endDateStr)){
				try {
					policy.setEndDateStr(sdf.format(sdf.parse(endDateStr)));
				} catch (ParseException e) {
					logger.info("日期格式轉失敗:",e); 
				}
			}
		}
		page.setData(list);
		Map<String,Object> resultMap = new HashMap<String, Object>();
		
		resultMap.put("EinvoiceCount", count);
		page.setTotal(count);
		page.setMap(resultMap);
		logger.debug("daoImpl:已开发票总数："+ count);
		return page;
	}
	
	/**
	 * 查询主页列表
	 */
	@Override
    public Page queryLifePolicyPageByCriteria(Page page) throws Exception {
        
        Map<String,Object> map = (Map<String, Object>) page.getType();
        page.setTotal((Integer)this.query("queryLifePolicyCountByMap", map));

        List<GatewayPolicy> list = queryList("queryLifePolicyPageByMap", map);
        
        //非即时起保保单保险止期应该是数据库所存日期的24:00:00
        /*SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
        for (GatewayPolicy tem : list) {
            GatewayPolicy policy = tem;
            String endDateStr = policy.getEndDateStr();
            if(StrUtil.isNotEmpty(endDateStr)){
                policy.setEndDateStr(sdf.format(sdf.parse(endDateStr)));
            }
        }*/
        
        page.setData(list);
        
        return page;
    }
	
	/**
	 * 查询主页列表
	 */
	@Override
    public Page queryScanPolicyPageByCriteria(Page page) throws Exception {
        
        Map<String,Object> map = (Map<String, Object>) page.getMap();
        page.setTotal((Integer)this.query("queryScanPolicyCountByMap", map));

        List<GatewayPolicy> list = queryList("queryScanPolicyPageByMap", map);
        
        page.setData(list);
        
        return page;
    }
	
	/**
	 * 导出Excel
	 */
	@Override
	public Map<String, String> queryScanPolicyAll(Map<String, String> map) throws Exception {
		
		return (Map<String, String>) query("queryCountOfScanByMap", map);
	}
	/**
     * 导出Excel
     */
    @Override
    public List<Map<String, String>> queryLifePolicyAll(Map<String, String> map) throws Exception {
        
        return queryList("queryCountOfLifeByMap", map);
    }
    /**
     * 导出Excel 界面出单
     */
    @Override
    public List<Map<String, Object>> queryLifeFacadePolicyListByCriteria(
            Map<String, Object> map) throws Exception {
        
        List<Map<String, Object>> lifePolicyList = queryList("queryPolicyFacadeListOfLifeByMap", map);
        
        //非即时起保保单保险止期应该是数据库所存日期的24:00:00
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
//        for (Map<String, Object> m : lifePolicyList) {
//            String endDateStr = (String)m.get("END_DATE");
//            
//            if(StrUtil.isNotEmpty(endDateStr) && !"null".equals(endDateStr)){
//                m.put("END_DATE", sdf.format(sdf.parse(endDateStr)));
//            }
//        }

        return lifePolicyList;
    }

	@Override
	public List<Map<String, Object>> queryScanPolicyListByCriteria(
			Map<String, Object> map) throws Exception {
		return queryList("queryScanPolicyListOfByMap", map);
	}

	@Override
	public GatewayPolicy queryGatewayPolicyByPolno(String polNo) throws Exception {
		return (GatewayPolicy) query("queryGatewayPolicyByPolno", polNo);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Map<String, String> queryOFDAndProofDownloadUrl(String policyId) throws Exception {
		
		return (Map<String,String>)  this.query("queryOFDAndProofDownloadUrl", policyId);
	}

	@Override
	public String querySingleEndorseByPolno(String polno) throws Exception {
		
		return (String) this.query("querySingleEndorseByPolno", polno);
	}
}
	 

	 

	 

 
